package com.castday2.castday2.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castday2.castday2.bean.User;
import com.castday2.castday2.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : 徐闯 mpcast
 * @Date : 2021-05-24 12:05:46
 * @Description : 分页查询的测试类
 **/
@RunWith(SpringRunner.class)
@SpringBootTest
public class A_TestPage {
    // 注入mapper
    @Autowired
    private UserMapper userMapper;
    // selectPage 分页
    @Test
    public  void demo1(){
        Page<User> page = new Page(2,3);
        IPage iPage = userMapper.selectPage(page,new QueryWrapper<User>()
            .eq("age",19)
        );
        System.out.println("数据条数==》"+iPage.getTotal());
        System.out.println("总页数==》"+iPage.getPages());
        System.out.println("当前页==>"+iPage.getCurrent());
        System.out.println("页长==>"+iPage.getSize());
        iPage.getRecords().forEach(System.out::println);
        iPage.getRecords().forEach(System.out::println);
    }

    @Test
    public void demo2(){
        //注意泛型  selectMapsPage 返回page  page中使用list封装记录  list泛型为Map  没一个map集合都是一条记录
        Page<Map<String,Object>> page = new Page(1,4);
        userMapper.selectMapsPage(page,new QueryWrapper<User>()
            .gt("age",10)
            .likeRight("name","z")
        );
        System.out.println("数据条数==》"+page.getTotal());
        System.out.println("总页数==》"+page.getPages());
        System.out.println("当前页==>"+page.getCurrent());
        System.out.println("页长==>"+page.getSize());
        List<Map<String,Object>> list = page.getRecords();
        list.forEach(System.out::println);
    }
}
